package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.pojo.Storage;
import com.gxa.zhonglong.pojo.StoreBarter;
import com.gxa.zhonglong.pojo.StoreReturn;
import com.gxa.zhonglong.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.*;

/**
 * 门店退货
 * @author wanjun
 * @version 1.0
 * @date 2021/10/25 16:21
 */
@Api(tags = "门店退货的接口")
@CrossOrigin
@RestController
@RequestMapping("/stroreReturn")
public class StroreReturnController {
    @Autowired
    GoodsTypeService goodsTypeService=null;
    @Autowired
    CodeService codeService=null;
    @Autowired
    StorageService storageService=null;
    @Autowired
    StoreBarterService storeBarterService=null;
    @Autowired
    StoreReturnService storeReturnService=null;
    @ApiOperation("批量编码添加退货单的方法")
    @PostMapping("/addcs")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "单据编号(必须)",name = "storeBarterNum",required = true),
            @ApiImplicitParam(value = "多个商品编码/ID数组(必须)",name = "goodsCoding",required = true),
            @ApiImplicitParam(value = "创单日期(非必须)",name = "storeBarterCtime"),
            @ApiImplicitParam(value = "新门店(必须)",name = "storeBarterNews",required = true),
            @ApiImplicitParam(value = "新柜台(必须)",name = "storeBarterNewc",required = true),
            @ApiImplicitParam(value = "创单人(必须)",name = "storeBarterCusername",required = true)
    })
    public Map<String,Object> addcs(StoreBarter storeBarter){
        if(storeBarter.getStoreBarterCtime()==null){
            storeBarter.setStoreBarterCtime(new Date());
        }
        System.out.println(storeBarter);
        System.out.println("看看");
        System.out.println(storeBarter.getGoodsCoding());
        System.out.println("对象:"+storeBarter);
        //拿到批量商品编码,并进行便历
        List<String> goodsCoding = (List<String>)storeBarter.getGoodsCoding();
        TreeSet<String> strings = new TreeSet<>(goodsCoding);
        //判断商品编码是否重复
        if(goodsCoding.size()!=strings.size()){
            return new JsonResult("0","商品编码重复","").getDataMap();
        }else {
            List<String> strings1=new ArrayList<>();
            //商品编码不重复判断库存中是否都存在
            for (String s : goodsCoding) {
                //编码查询商品信息
                Map<String, Object> map = storeBarterService.selectGoodByCode(s);
                if(map==null){
                    return new JsonResult("0","编码为"+s+"的商品不存在","").getDataMap();
                }else {
                    String s1 = storeBarterService.selectNumByGoodCoding(s);
                    if(s1!=null){
                        return new JsonResult("0","编码为"+s+"的商品已在审批单中","").getDataMap();
                    }
                    //将商品编码记录下来,准备存入数据表
                    strings1.add((String)map.get("storage_jewelry_code"));
                    //1.添加退货表记录,传对象
                    //2.新建关联表记录,传list和单据ID
                    //3.两个方法合成一个才可以service中构成事物
                    storeReturnService.add(storeBarter,storeBarter.getStoreBarterNum(),strings1);
                }
            }
            return new JsonResult("1","成功","").getDataMap();
        }

    }

    @ApiOperation("查询全部退货单的方法(分页)")
    @PostMapping("/selectAll")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码(必须)", name = "offset",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(value = "每页显示条数(必须)", name = "size",required = true,dataType = "int",paramType = "query")
    })
    public Map selectAll(Integer offset,Integer size){
        System.out.println(offset);
        System.out.println(size);
        List<StoreBarter> storeBarters = storeReturnService.selectAll(offset, size);
        //不分页的方法获得count
        List<StoreBarter> storeBarters2 = storeReturnService.selectAll2();
        int count = storeBarters2.size();
        return new JsonResult("1","成功",storeBarters,count).getDataMap();
    }


    @ApiOperation("多条件查询退货单的方法")
    @PostMapping("/selectByField")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "创单日期from(非必须)",name = "from"),
            @ApiImplicitParam(value = "创单日期to(非必须)",name = "to"),
            @ApiImplicitParam(value = "商品类型(非必须)",name = "storeBarterGclass"),
            @ApiImplicitParam(value = "入库门店(非必须)",name = "storeBarterNews"),
            @ApiImplicitParam(value = "入库柜台(非必须)",name = "storeBarterNewc"),
            @ApiImplicitParam(value = "出库门店(非必须)",name = "storeBarterOlds"),
            @ApiImplicitParam(value = "出库柜台(非必须)",name = "storeBarterOldc"),
            @ApiImplicitParam(value = "创单人(非必须)",name = "storeBarterCusername"),
            @ApiImplicitParam(value = "页码(必须)",name = "offset",required = true),
            @ApiImplicitParam(value = "每页条数(必须)",name = "size",required = true)
    })
    public Map selectByField( Timestamp from,  Timestamp to,
                              String storeBarterGclass,  String storeBarterNews,
                              String storeBarterNewc,  String storeBarterOlds,
                              String storeBarterOldc,  String storeBarterCusername,
                              Integer offset,  Integer size){

        List<StoreBarter> storeBarters = storeReturnService.selectByField(from,to, storeBarterGclass,
                storeBarterNews, storeBarterNewc, storeBarterOlds, storeBarterOldc, storeBarterCusername,
                offset, size);
        List<StoreBarter> storeBarters2 = storeReturnService.selectByField2(from,to, storeBarterGclass,
                storeBarterNews, storeBarterNewc, storeBarterOlds, storeBarterOldc, storeBarterCusername);
        int count = storeBarters2.size();
        return new JsonResult("1","成功",storeBarters,count).getDataMap();
    }


    @ApiOperation("审核通过的方法")
    @PostMapping("/pass")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "退货表表单编码(必须)",name = "storeBarterNum",required = true) ,
            @ApiImplicitParam(value = "审批人(必须)",name = "storeBarterEusername",required = true)
    })
    public Map pass(String storeBarterNum,String storeBarterEusername){
        Timestamp storeBarterEtime = new Timestamp(System.currentTimeMillis());
        storeBarterService.updateState1(storeBarterNum,storeBarterEusername,storeBarterEtime);
        return new JsonResult("1","成功","").getDataMap();
    }

    @ApiOperation("驳回审核的方法")
    @PostMapping("/reject")
    @ApiImplicitParam(value = "退货表单据编号(必须)",name = "storeBarterNum",required = true)
    public Map reject(@RequestParam(name="storeBarterNum" ) String storeBarterNum){
        storeBarterService.updateState2(storeBarterNum);
        return new JsonResult("1","成功","").getDataMap();
    }

    @ApiOperation("撤销驳回的方法")
    @PostMapping("/backout")
    @ApiImplicitParam(value = "退货表单据编码(必须)",name = "storeBarterNum",required = true)
    public Map backout(@RequestParam(name="storeBarterNum" ) String storeBarterNum){
        storeBarterService.updateState0(storeBarterNum);
        return new JsonResult("1","成功","").getDataMap();
    }

    @ApiOperation("删除退货单的方法")
    @PostMapping("/delete")
    @ApiImplicitParam(value = "退货单单据编码(必须)",name = "storeBarterNum",required = true)
    public Map delete( String storeBarterNum){
        storeBarterService.delete(storeBarterNum);
        return new JsonResult("1","成功","").getDataMap();
    }


    @ApiOperation("修改退货单信息的方法")
    @PostMapping("/update")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "退货单单据编码(必须,另外3个选)",name = "storeBarterNum",required = true),
            @ApiImplicitParam(value = "创单日期(非必须)",name = "storeBarterCtime"),
            @ApiImplicitParam(value = "入库总部(非必须)",name = "storeBarterNews"),
            @ApiImplicitParam(value = "入库类库(非必须)",name = "storeBarterNewc"),
    })

    public Map update( String storeBarterNum, Timestamp storeBarterCtime,
                       String storeBarterNews, String storeBarterNewc){
        storeBarterService.updateBarter(storeBarterNum, storeBarterCtime, storeBarterNews, storeBarterNewc);
        return new JsonResult("1","成功","").getDataMap();
    }

    @ApiOperation("商品编码查询商品详情的方法")
    @PostMapping("/selectGoodDeta")
    @ApiImplicitParam(value = "商品编码(必须)",name = "goodCoding",required = true)
    public Map selectGoodDeta(@RequestParam(name="goodCoding" ) String goodCoding){
        Map map = storeBarterService.selectGoodByCode(goodCoding);
        if(map==null){
            return new JsonResult("0","编码为"+goodCoding+"的商品不存在","").getDataMap();
        }else {
            String s = storeBarterService.selectNumByGoodCoding(goodCoding);
            if(s!=null){
                return new JsonResult("0","该商品已在审批单中","").getDataMap();
            }
            return new JsonResult("1","成功",map).getDataMap();
        }

    }

    @ApiOperation("多商品编码查询商品详情的方法")
    @PostMapping("/selectGoodsDetas")
    @ApiImplicitParam(value = "多商品编码(必须)",name = "goodsCoding",required = true)
    public Map selectGoodsDeta(StoreBarter storeBarter){
        System.out.println(storeBarter);
        //拿到批量商品编码,并进行便历
        List<String> goodsCoding = (List<String>)storeBarter.getGoodsCoding();
        System.out.println(goodsCoding);
        System.out.println("---------------------------------------");
        TreeSet<String> strings = new TreeSet<>(goodsCoding);
        //判断商品编码是否重复
        if(goodsCoding.size()!=strings.size()){
            return new JsonResult("0","商品编码重复","").getDataMap();
        }else {
            List<Map> goodsList=new ArrayList<>();
            //商品编码不重复判断库存中是否都存在
            for (String s : goodsCoding) {
                //编码查询商品信息
                System.out.println("List是:"+goodsCoding);
                System.out.println("s是:"+s);
                Map map = storeBarterService.selectGoodByCode(s);
                if(map==null){
                    return new JsonResult("0","编码为"+s+"的商品不存在","").getDataMap();
                }else {
                    String s1 = storeBarterService.selectNumByGoodCoding(s);
                    if(s1!=null){
                        return new JsonResult("0","编码为"+s+"的商品已在审批单中","").getDataMap();
                    }
                    goodsList.add(map);
                }
            }
            System.out.println(goodsList);
            return new JsonResult("1","成功",goodsList).getDataMap();
        }
    }

    //调用别人的方法,参数???
    @ApiOperation("从库存条件查询的方法")
    @PostMapping("/selectFromDepot")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storageJewelryCode",value = "商品编码(非必须)",dataType = "string"),
            @ApiImplicitParam(name = "storageJewelryName",value = "商品名称(非必须)",dataType = "string"),
            @ApiImplicitParam(name = "storageStore",value = "门店名称(非必须)",dataType = "string"),
            @ApiImplicitParam(name = "offset",value = "页数(必须)",dataType = "int",required = true),
            @ApiImplicitParam(name = "size",value = "每页记录数(必须)",dataType = "int",required = true)
    })
    public Map selectFromDepot(String storageJewelryCode, String storageJewelryName, String storageStore, Integer offset, Integer size){
        List<Storage> storages = storageService.select(storageJewelryCode, storageJewelryName, storageStore,offset, size);
        Integer count=storages.size();
        return new JsonResult("1","成功",storages,count).getDataMap();
    }



    @ApiOperation("单据编码查包含商品信息的方法")
    @PostMapping("/selectBaDeta")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "退货表单据编码(必须)",name = "storeBarterNum",required = true),
            @ApiImplicitParam(value = "页码(必须)", name = "offset",required = true),
            @ApiImplicitParam(value = "每页显示条数(必须)", name = "size",required = true)
    })
    public Map selectBaDeta(String storeBarterNum,Integer offset,Integer size){
        System.out.println(offset+size);
        List<Map<String, Object>> list = storeBarterService.selectBaDeta(storeBarterNum, offset, size);
        List<Map<String, Object>> list1 = storeBarterService.selectBaDeta1(storeBarterNum);
        Integer count=list1.size();
        return new JsonResult("1","成功",list,count).getDataMap();
    }


    @ApiOperation("删除退货单包含商品信息的方法")
    @PostMapping("/deleteBagood")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "退货表单据编码(必须)",name = "storeBarterNum",required = true),
            @ApiImplicitParam(value = "商品的单据编码(必须)",name = "goodCoding",required = true)
    })
    public Map deleteBagood(String storeBarterNum,String goodCoding){
        ////////////////////////////////////////////////////////
        //退货单里边如果没有商品则是查询不到该退货单

        ///////////////////////////////////////////////////////
        System.out.println(storeBarterNum);
        System.out.println(goodCoding);
        storeBarterService.deleteBagood(storeBarterNum, goodCoding);
        return new JsonResult("1","成功","").getDataMap();
    }


    @ApiOperation("查询全部总部的方法")
    @PostMapping("/selectAllStore")
    public Map selectAllStore(){
        List<String> allHead = storeReturnService.getAllHead();
        return new JsonResult("1","成功",allHead).getDataMap();
    }



    @ApiOperation("查询总部下类库的方法")
    @PostMapping("/selectCounter")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "总部名字(必须)",name = "CName",required = true),
    })
    public Map selectCounter(String CName) throws Exception {
        List<String> classLibName = storeReturnService.getClassLibName(CName);
        return new JsonResult("1","成功",classLibName).getDataMap();
    }

    @ApiOperation("生成一个门店退货单据编码")
    @PostMapping("/getSTRCode")
    public Map selectCounter()  {
        String generation = codeService.generation(4L);
        return new JsonResult("1","成功",generation).getDataMap();
    }


    @ApiOperation("获得全部商品类型名字")
    @PostMapping("/getNameList")
    public Map getNameList()  {
        List<String> nameList = goodsTypeService.getNameList();
        return new JsonResult("1","成功",nameList).getDataMap();
    }


}


